var Login = function () {
    return {
        //main function to initiate the module
        init: function (loginOk, checkOk) {
        	
           $('.login-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            rules: {
	                username: {
	                    required: true
	                },
	                password: {
	                    required: true
	                }
	            },

	            messages: {
	                username: {
	                    required: ""
	                },
	                password: {
	                    required: ""
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   
//	                $('.alert-error', $('.login-form')).show();
	            	$('.alert-error > span').html("请输入用户名与密码");
	                $(".alert-error").slideDown(100);
    				setTimeout(function() {
						$(".alert-error").slideUp(100);
					}, 1000);
	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	            	if ($('.login-form').validate().form()) {
	            		loginAjax();
	                }
	            }
	        });

	        $('.login-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.login-form').validate().form()) {
	                	loginAjax();
	                }
	                return false;
	            }
	        });
	        
	        
	        function loginAjax() {
	        	$.ajax({
		    		"type" : "post",
		    		"url" : "main/login.mvc",
		    		"dataType" : "json",
		    		"data" : {
		    			"loginName" : $("input[name='username']").val(),
		    			"loginPassword" : $("input[name='password']").val()
		    		},
		    		"success" : function(resp) {
		    			if (resp.msg == loginOk) {
		    				window.location.href = "forwardPage.mvc?page=index";
		    			} else if (resp.msg == checkOk) {
		    				$("#_checkLoginSeize").modal("show");
		    			} else {
		    				$('.alert-error > span').html(resp.msg);
		    				$(".alert-error").slideDown(100);
		    				setTimeout(function() {
								$(".alert-error").slideUp(100);
							}, 1000);
		    			}
		    		}
	        	});
	        };

			//弹出抢登，回车确认
	        document.getElementById("_checkLoginSeize").onkeyup = function () {
				if(event.keyCode==13){
					$("#_checkLoginSeize .btn.green").click();
				}
			};

	        jQuery("#_checkLoginSeize .btn.green").click(function() {
	        	$.ajax({
					  type:'GET',
					  url: 'main/loginSession.mvc',
					  dataType: "json",
					  data : {
		        			"loginName" : $("input[name='username']").val()
		        	  },
					  success:function(data){
	        			  window.location.href = "forwardPage.mvc?page=index";
					  }
				});
	        }); 

	       //忘记密码
	        $('.forget-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            ignore: "",
	            rules: {
	                email: {
	                    required: true,
	                    email: true
	                }
	            },

	            messages: {
	                email: {
	                    required: "Email is required."
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   

	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	                window.location.href = "index.jsp";
	            }
	        });

	        $('.forget-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.forget-form').validate().form()) {
	                    window.location.href = "index.jsp";
	                }
	                return false;
	            }
	        });

	        jQuery('#forget-password').click(function () {
	            jQuery('.login-form').hide();
	            jQuery('.forget-form').show();
	        });

	        jQuery('#back-btn').click(function () {
	            jQuery('.login-form').show();
	            jQuery('.forget-form').hide();
	        });

	        //注册
	        $('.register-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            ignore: "",
	            rules: {
	                username: {
	                    required: true
	                },
	                password: {
	                    required: true
	                },
	                rpassword: {
	                    equalTo: "#register_password"
	                },
	                email: {
	                    required: true,
	                    email: true
	                },
	                tnc: {
	                    required: true
	                }
	            },

	            messages: { // custom messages for radio buttons and checkboxes
	                tnc: {
	                    required: "Please accept TNC first."
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   

	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                if (element.attr("name") == "tnc") { // insert checkbox errors after the container                  
	                    error.addClass('help-small no-left-padding').insertAfter($('#register_tnc_error'));
	                } else {
	                    error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	                }
	            },

	            submitHandler: function (form) {
	                window.location.href = "index.html";
	            }
	        });

	        jQuery('#register-btn').click(function () {
	            jQuery('.login-form').hide();
	            jQuery('.register-form').show();
	        });

	        jQuery('#register-back-btn').click(function () {
	            jQuery('.login-form').show();
	            jQuery('.register-form').hide();
	        });
        }
    
    };

    

}();